using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using WalkingTec.Mvvm.Core;
using UUIDNext;
using HengTong.Model.Db.SupplyChain.Purchase;


namespace HengTong.ViewModel.SupplyChain.SysPurchaseRequirementPoolVms
{
    public partial class SysPurchaseRequirementPoolVm : CrudVm<SysPurchaseRequirementPool>
    {
        [Display(Name = "需求池物料分类")]
        public List<string> SelectedMaterialGroupsIds { get; set; }
        [Display(Name = "采购需求池管理人员关联表")]
        public List<string> SelectedManagersIds { get; set; }

        public SysPurchaseRequirementPoolVm()
        {
            SetInclude(x => x.PurchaserInCharge);
            SetInclude(x => x.Items);
            SetInclude(x => x.MaterialGroups);
            SetInclude(x => x.Managers);
        }

        protected override void InitVM()
        {
            SelectedMaterialGroupsIds = Entity.MaterialGroups?.Select(x => x.GroupId.ToString()).ToList();
            SelectedManagersIds = Entity.Managers?.Select(x => x.ManagerId.ToString()).ToList();
        }

        public override async Task DoAddAsync()
        {
            if (Entity is IBasePoco poco)
            {
                poco.CreateBy = Wtm.LoginUserInfo?.ITCode;
                poco.UpdateBy = Wtm.LoginUserInfo?.ITCode;
                poco.CreateTime = DateTime.Now;
                poco.UpdateTime = DateTime.Now;
            }
            Entity.MaterialGroups = new List<SysPurchaseRequirementPoolMaterialGroup>();
            if (SelectedMaterialGroupsIds != null)
            {
                foreach (var id in SelectedMaterialGroupsIds)
                {
                     SysPurchaseRequirementPoolMaterialGroup middle = new SysPurchaseRequirementPoolMaterialGroup();
                    middle.SetPropertyValue("GroupId", id);
                    Entity.MaterialGroups.Add(middle);
                }
            }

            Entity.Managers = new List<SysPurchaseRequirementPoolManager>();
            if (SelectedManagersIds != null)
            {
                foreach (var id in SelectedManagersIds)
                {
                     SysPurchaseRequirementPoolManager middle = new SysPurchaseRequirementPoolManager();
                    middle.SetPropertyValue("ManagerId", id);
                    Entity.Managers.Add(middle);
                }
            }

            Entity.ID = Uuid.NewDatabaseFriendly(Database.SqlServer);
            await base.DoAddAsync();
        }

        public override async Task DoEditAsync(bool updateAllFields = false)
        {
            if (Entity is IBasePoco poco)
            {
                poco.UpdateBy = Wtm.LoginUserInfo?.ITCode;
                poco.UpdateTime = DateTime.Now;
            }
            Entity.MaterialGroups = new List<SysPurchaseRequirementPoolMaterialGroup>();
            if(SelectedMaterialGroupsIds != null )
            {
                 foreach (var item in SelectedMaterialGroupsIds)
                {
                    SysPurchaseRequirementPoolMaterialGroup middle = new SysPurchaseRequirementPoolMaterialGroup();
                    middle.SetPropertyValue("GroupId", item);
                    Entity.MaterialGroups.Add(middle);
                }
            }

            Entity.Managers = new List<SysPurchaseRequirementPoolManager>();
            if(SelectedManagersIds != null )
            {
                 foreach (var item in SelectedManagersIds)
                {
                    SysPurchaseRequirementPoolManager middle = new SysPurchaseRequirementPoolManager();
                    middle.SetPropertyValue("ManagerId", item);
                    Entity.Managers.Add(middle);
                }
            }

            await base.DoEditAsync(updateAllFields);
        }

    }
}
